package com.zonski.jbots.game.collision;

import com.zonski.jbots.engine.*;

import com.zonski.jbots.game.JBotsConstants;

/**
 * Collision that sucks power from an entity BATTERY, if the amount is negative
 * then the power is sucked from the object being collided withs battery
 */
public class ChargeSappingCollisionEffect implements CollisionEffect
{
    public static final String DESTRUCTIVE_COLLISION   = "dest_collision";
    public static final String SAPPING_COLLISION       = "sap_collision";
    private static final int MAX_PAIN = 10;
    private int amount;
    private boolean sounds;

    public ChargeSappingCollisionEffect(int amount, boolean sounds)
    {
        this.amount = amount;
        this.sounds = sounds;
    }

    public boolean apply(Entity entity, Engine engine, CollisionData collision)
    {
        if(this.amount > 0) // equally destructive collision
        {
            if(this.sounds)
            {
                entity.getDevice().play(DESTRUCTIVE_COLLISION, 1);
            }
            // show the pain (fool)
            // the more pain the more stars
            int painMod = MAX_PAIN - this.amount;
            if(painMod <= 0 || entity.getEffectiveModeUpdates()%painMod == 0 ||
                    collision.edge != Directions.NONE)
            {
                try
                {
                    Entity pain = engine.getEntityFactory().createEntity(JBotsConstants.PAIN);
                    pain.setMode(Modes.DEAD);
                    pain.setEffectiveMode(Modes.DEAD);
                    pain.bounds.setCentre(entity.bounds);
                    pain.setVelocity(JBotsConstants.RANDOM.nextInt() % 3, JBotsConstants.RANDOM.nextInt() % 3, 0);
                    pain.layerId = entity.layerId;
                    engine.getRoom().addEntity(pain);
                }catch(Exception ex){
                    System.out.println(ex);
                }
            }
            entity.setCharge(entity.getCharge() - this.amount);
        }else{
            // power sapping collision
            int maxCharge = entity.getMaxCharge();
            if(entity.getCharge() < maxCharge)
            {
                int min = Math.min(-this.amount, maxCharge - entity.getCharge());
                Entity with = collision.with;
                min = Math.min(min, with.getCharge());
                if(min < 0)
                {
                    min = 0;
                }
                entity.setCharge(entity.getCharge() + min);
                with.setCharge(with.getCharge() - min);
                if(min > 0 && this.sounds)
                {
                    entity.getDevice().play(SAPPING_COLLISION, 1);
                }
            }
        }
        return false;
    }

    public int getCollisionType(Entity entity, Entity with)
    {
        int collisionType;
        if(entity.getMode() == Modes.DEAD || with.getMode() == Modes.DEAD ||
                entity.isRemovable() || with.isRemovable())
        {
            collisionType = NO_COLLISIONS;
        }else{
            collisionType = SIMPLE_COLLISIONS;
        }
        return collisionType;
    }
}
